package core.september.cloudbadge.vcard.badgemodel.fields;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.Embeddable;

import core.september.cloudbadge.vcard.badgemodel.Constants;
import ezvcard.parameters.AddressTypeParameter;
import ezvcard.parameters.EmailTypeParameter;
import ezvcard.parameters.TelephoneTypeParameter;
import ezvcard.types.AddressType;
import ezvcard.types.CategoriesType;
import ezvcard.types.EmailType;
import ezvcard.types.GeoType;
import ezvcard.types.TelephoneType;
import ezvcard.types.TitleType;
import ezvcard.types.UrlType;


public class WorkingField extends AbsField{
	
	
	private final static String[] _socialUrls = new String[]{Constants._fb,Constants._in,Constants._tw};
	
	private final static AddressTypeParameter _ATP = AddressTypeParameter.WORK;
	private final static TelephoneTypeParameter _TTP = TelephoneTypeParameter.WORK;
	private final static EmailTypeParameter _ETP = EmailTypeParameter.WORK;
	
	
	

	public String getTitle() {
		String title = null;
		for(TitleType tt: getVcard().getTitles()) {
			if(title == null) title = "";
			title+= tt.getValue()+" ";
		}
		return title;
	}



	public void setTitle(String title) {
		getVcard().addTitle(title);
	}



	public String getOrganization() {
		String organization = null;
		for(String ot: getVcard().getOrganization().getValues()) {
			if(organization == null) organization = "";
			organization+= ot;
		}
		return organization;
	}



	public void setOrganization(String organization) {
		getVcard().setOrganization(organization);
	}



	public AddressType getAdr() {
		AddressType adr = null;
		for(AddressType at : getVcard().getAddresses()) {
			for(AddressTypeParameter atp: at.getTypes()) {
				if(atp.equals(_ATP)) {
					adr = at;
				}
			}
		}
		if (adr == null) {
			adr = new AddressType();
			adr.addType(_ATP);
			setAdr(adr);
		}
		return adr;
	}



	public void setAdr(AddressType adr) {
		getVcard().addAddress(adr);
	}

	private String getTelephone(TelephoneTypeParameter parameter) {
		for(TelephoneType tt : getVcard().getTelephoneNumbers()) {
			for(TelephoneTypeParameter ttp: tt.getTypes()) {
				if(ttp.equals(parameter)){
					return ttp.getValue();
				}
			}
		}
		return null;
	}

	public String getTelephone() {
		return getTelephone(TelephoneTypeParameter.VOICE);
	}



	public void setTelephone(String telephone) {
		getVcard().addTelephoneNumber(telephone,_TTP, TelephoneTypeParameter.VOICE);
	}



	public String getMobile() {
		return getTelephone(TelephoneTypeParameter.CELL);
	}



	public void setMobile(String mobile) {
		getVcard().addTelephoneNumber(mobile,_TTP, TelephoneTypeParameter.CELL);
	}



	public String getFax() {
		return getTelephone(TelephoneTypeParameter.FAX);
	}



	public void setFax(String fax) {
		getVcard().addTelephoneNumber(fax,_TTP, TelephoneTypeParameter.FAX);
	}



	public String getUrl() {
		for(UrlType t : getVcard().getUrls()) {
			Boolean isSocial = false;
			for(String social : _socialUrls) {
				if(t.getValue().contains(social))
					isSocial = true;
			
			}
			if(!isSocial) {}
			return t.getValue();
		}
		return null;
	}



	public void setUrl(String url) {
		getVcard().addUrl(url);
	}


	private String getSocial(String social) {
		for(UrlType t : getVcard().getUrls()) {
			if(t.getValue().contains(social))
				return t.getValue();
		}
		return null;
	}

	public String getFacebook() {
		return getSocial(Constants._fb);
	}



	public void setFacebook(String facebook) {
		getVcard().addUrl(facebook);
	}



	public String getTwitter() {
		return getSocial(Constants._tw);
	}



	public void setTwitter(String twitter) {
		getVcard().addUrl(twitter);
	}



	public String getLinkedin() {
		return getSocial(Constants._in);
	}



	public void setLinkedin(String linkedin) {
		getVcard().addUrl(linkedin);
	}



	public GeoType getGeo() {
		return getVcard().getGeo();
	}



	public void setGeo(double latitude, double longitude) {
		getVcard().addGeo(new GeoType(latitude, longitude));
	}



	public List<String> getCategories() {
		List<String> retType = null;
		for(CategoriesType ct : getVcard().getCategoriesList()) {
			for(String value : ct.getValues()) {
				if(retType == null) retType = new LinkedList<String>();
				retType.add(value);
			}
			
		}
		return retType;
	}



	public void setCategories(String... categories) {
		getVcard().setCategories(categories);
	}

	

	public String getStreetAddress() {
		return getAdr().getStreetAddress();
	}



	public void setStreetAddress(String streetAddress) {
		getAdr().setStreetAddress(streetAddress);
	}



	public String getLocality() {
		return getAdr().getLocality();
	}



	public void setLocality(String locality) {
		getAdr().setLocality(locality);
	}



	public String getPostalCode() {
		return getAdr().getPostalCode();
	}



	public void setPostalCode(String postalCode) {
		getAdr().setPostalCode(postalCode);
	}



	public String getCountry() {
		return getAdr().getCountry();
	}



	public void setCountry(String country) {
		getAdr().setCountry(country);
	}



	public String getRegion() {
		return getAdr().getRegion();
	}



	public void setRegion(String region) {
		getAdr().setRegion(region);
	}

	public String getMail() {
		for(EmailType et : getVcard().getEmails()) {
			if(et.getTypes().contains(_ETP)) {
				return et.getValue();
			}
		}
		return null;
	}
	
	public void setMail(String mailAddress) {
		getVcard().addEmail(mailAddress, _ETP);
	}


	@Override
	public void commit() {
		getAdr().setLabel(getStreetAddress()+System.getProperty("line.separator")+
				getLocality()+", "+getRegion()+" "+getPostalCode()+System.getProperty("line.separator")+
				getCountry());
	}

}
